package com.blog.module.photos.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.blog.common.config.FileConfig;
import com.blog.common.controller.BaseAdminController;
import com.blog.common.mybatis.Page;
import com.blog.common.utils.FileUtil;
import com.blog.module.photos.pojo.Photos;
import com.blog.module.photos.pojo.PhotosInfo;
import com.blog.module.photos.service.PhotosInfoService;
import com.blog.module.photos.service.PhotosService;

@Controller
@RequestMapping("/admin/photosinfo/**")
public class PhotosInfoAdminController extends BaseAdminController {

	@Autowired
	private PhotosInfoService photosInfoService;

	@Autowired
	private PhotosService photosService;

	@RequestMapping(value = "/list")
	public String pagePhotosInfo(PhotosInfo photosInfo, Model model) {
		Page page = photosInfoService.selectPhotosInfoByPhotosId(photosInfo);
		List<Photos> photoList = photosService.selectPhotosList();
		model.addAttribute("page", page);
		model.addAttribute("photoList", photoList);
		return "/admin/photosinfo/list";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(Model model) {
		List<Photos> photoList = photosService.selectPhotosList();
		model.addAttribute("photoList", photoList);
		return "/admin/photosinfo/add";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(PhotosInfo photosInfo, @RequestParam(value = "image", required = false) CommonsMultipartFile file) {
		String imagePath = "";
        if (!file.isEmpty()) {
            imagePath = FileUtil.uploadFile(file, FileConfig.PHOTOS);
            photosInfo.setUrl(imagePath);
        }
		photosInfoService.insert(photosInfo);
		return redirect("/admin/photosinfo/list");
	}

	@RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
	public String edit(Model model, @PathVariable Integer id) {
		List<Photos> photoList = photosService.selectPhotosList();
		PhotosInfo photosInfo = photosInfoService.selectById(id);

		model.addAttribute("photoList", photoList);
		model.addAttribute("photosInfo", photosInfo);
		return "/admin/photosinfo/edit";
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public String edit(PhotosInfo photosInfo, @RequestParam(value = "image", required = false) CommonsMultipartFile file) {
		String imagePath = "";
        if (!file.isEmpty()) {
            imagePath = FileUtil.uploadFile(file, FileConfig.PHOTOS);
            photosInfo.setUrl(imagePath);
        }
		photosInfoService.update(photosInfo);
		return redirect("/admin/photosinfo/list");
	}
	
	@RequestMapping(value = "/{id}/del", method = RequestMethod.POST)
	public String edit(@PathVariable Integer id) {
		photosInfoService.del(id);
		return redirect("/admin/photosinfo/list");
	}
	
}
